<?php

namespace lib\Tool;

use lib\rule\ParametHandle;

/**
 * Ldy-item-Tools
 * 工具类(工具全部通过My方法调用)
 * Class Tools
 * @package lib\Tool
 */
class Tools
{

    public static $Myobject;

    public static function My()
    {
        if (empty(self::$Myobject)) {
            self::$Myobject = new Tools();
        }
        return self::$Myobject;
    }

    /**
     * 打印类
     * @return string
     */
    public function output()
    {
        $res = '';
        $datas = func_get_args();
        foreach ($datas as $key => $value) {
            $res .= '<pre style="display: block;padding: 9.5px;margin: 40px 0px 10px 0px;font-size: 13px;line-height: 1.42857;color: #333;word-break: break-all;word-wrap: break-word;background-color: #F5F5F5;border: 1px solid #CCC;border-radius: 4px;">' . print_r($value, true) . '</pre>';
        }
        return $res;
    }

    /**
     * 参数处理类
     * @return ParametHandle
     */
    public function request()
    {
        static $instance;
        if (empty($instance)) {
            $instance = new ParametHandle();
        }

        return $instance;
    }


    public function _csrf()
    {
        $user_ip = $this->get_real_ip();
        
    }

    public function url($url =''){
        $urlData = explode('/',$url);
        return 'index.php?c='.$urlData[0].'&m='.$urlData[1];
    }

    /**
     * 获取用户IP
     * @return bool|int|mixed
     */
    public function get_real_ip(){
        $ip = rand(100,999);
        if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        }
        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ips = explode(", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
            if ($ip) {
                array_unshift($ips, $ip);
                $ip = FALSE;
            }
            for ($i = 0; $i < count($ips); $i++) {
                if (!eregi("^(10│172.16│192.168).", $ips[$i])) {
                    $ip = $ips[$i];
                    break;
                }
            }
        }
        return $ip ? $ip : $_SERVER['REMOTE_ADDR'];
    }
}